﻿/**************************************************************** 
 *  作者：       sq1000
 *  创建时间：   2012-06-13 11:43:10 AM
 *
 ***************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasyMvcBlog.Data;
using EasyMvcBlog.DTO;
using EasyMvcBlog.Infrastructure.Repository.DataRepository;
using AutoMapper;

namespace EasyMvcBlog.Domain
{
    public class CommentDomain
    {
        #region - Variables -

        CommentRepository commentRepository = new CommentRepository();

        #endregion

        #region - Delegate -

        public delegate bool CommentHandle(Comment c);
        public event CommentHandle BeforeComment;
        public event CommentHandle AfterComment;

        #endregion

        #region - Methods -

        public bool AddComment(CommentDTO comment)
        {
            Mapper.CreateMap<CommentDTO, Comment>();
            Comment c = Mapper.Map<CommentDTO, Comment>(comment);

            if (BeforeComment != null && !BeforeComment(c))
            {
                return false;
            }

            commentRepository.Add(c);

            if (AfterComment != null)
            {
                AfterComment(c);
            }

            return c.Id > 0;
        }

        public List<CommentDTO> GetArticleComment(int articleId)
        {
            Mapper.CreateMap<Comment, CommentDTO>();
            List<Comment> lists = commentRepository.GetList(o => o.Article.Id == articleId).OrderBy(o => o.CreatedTime).ToList();
            return Mapper.Map<List<Comment>, List<CommentDTO>>(lists);
        }
        #endregion

        public bool Delete(int commentId)
        {
            commentRepository.DeleteById<Comment>(commentId);
            return commentRepository.GetById(commentId) == null;
        }
    }
}
